package com.younion.action.manage;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;

import com.younion.common.base.BaseAction;
import com.younion.common.constants.RoleType;
import com.younion.common.paginator.Paginator;

import cpcns.jhsks.util.MapCollectionUtils;

import com.younion.business.ICustomerAccountService;
import com.younion.vo.CustomerAccountVO;

/**
 * @author wells
*/
public class CustomerAccountAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	protected static final Log logger = LogFactory.getLog(CustomerAccountAction.class);

	private ICustomerAccountService customerAccountService = null;

	private CustomerAccountVO customerAccountVO = new CustomerAccountVO();

	private List<CustomerAccountVO> customerAccountList;
	/**
	 * 列表：翻页区；数据区
	 */
	@SuppressWarnings("unchecked")
	@RequiresPermissions("listCustomerAccount")
	public String listCustomerAccount() {
		try {
			
			Map paramMap = null;
			if (customerAccountVO != null) {
				paramMap = MapCollectionUtils.removeEmptyEntry(PropertyUtils.describe(customerAccountVO));
			}
			// 获取总记录数
			int total = customerAccountService.getCountByMap(paramMap);
			// 根据总记录数获取分页对象
			Paginator paginator = this.getPaginator(total);
			customerAccountList = customerAccountService.get(paramMap, paginator);
			// 保存分页对象
			this.savaPaginator(paginator);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return SUCCESS;
	}

	/**
	 * 新增
	 */
	@RequiresPermissions("addCustomerAccount")
	public String addCustomerAccount() {
		return SUCCESS;
	}

	/**
	 *修改保存，调度Jsp
	 */
	@RequiresPermissions("editCustomerAccount")
	public String editCustomerAccount() {
		try {
			Map paramMap = null;
			if (customerAccountVO != null) {
				paramMap = MapCollectionUtils.removeEmptyEntry(PropertyUtils.describe(customerAccountVO));
			}
			List<CustomerAccountVO> list = (List<CustomerAccountVO>) customerAccountService.getByMap(paramMap);
			if(list!=null&&list.size()>0){
				customerAccountVO = list.get(0);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}

	/**
	 * 保存动作
	 */
	@RequiresPermissions("listCustomer")
	public String saveCustomerAccount() {
		try {
			customerAccountVO = customerAccountService.save(customerAccountVO);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
		}
		return SUCCESS;
	}

	/**
	 * 删除
	 */
	@RequiresPermissions("deleteCustomerAccount")
	public String deleteCustomerAccount() {
		try {
			String pkId = request.getParameter("ids");
			customerAccountService.remove(Arrays.asList(pkId.split(",")));
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
		}
		return SUCCESS;
	}
	/**
	 * 查看鿴
	 */
	@RequiresPermissions("viewCustomerAccount")
	public String viewCustomerAccount() {
		try {
			String pkId = request.getParameter("ids");
			customerAccountVO = customerAccountService.getById(pkId);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
		}
		return SUCCESS;

	}

	public CustomerAccountVO getCustomerAccountVO() {
		return customerAccountVO;
	}

	public void setCustomerAccountVO(CustomerAccountVO customerAccountVO) {
		this.customerAccountVO = customerAccountVO;
	}

	public void setCustomerAccountService(ICustomerAccountService customerAccountService) {
		this.customerAccountService = customerAccountService;
	}

	public List<CustomerAccountVO> getCustomerAccountList() {
		return customerAccountList;
	}

	public void setCustomerAccountList(List<CustomerAccountVO> customerAccountList) {
		this.customerAccountList = customerAccountList;
	}

}